* extra regressions not shown in paper:
*   1) using WDI GDP ínstead PWT GDP
*   2) no endogenous e0 or asr
*	3) no yr_sch
*   4) no csr (just yr_sch and adult survival)

cd "~/Documents/econ/research/current/cognitive/estimation/dofiles"

use ../data/growth_panel, clear
xtset isonum year, delta(5)

label var lgdppw "Initial log GDP p.w."
label var lgdppwWDI "Initial log GDP p.w."
label var PRI "Institutions (0-100)"
label var trade_open "Trade openness (\% of GDP)"
label var e0 "Life expectancy (years)"
label var yr_sch "Schooling (years)"
label var csr "Child survival (per 1000)"
label var asr "Adult survival (per 1000)"
label var csr_lag "Child survival, previous generation"
label var isr_lag "Infant survival, lagged (per 1000)"

gen csr_lo = csr_lag*(wb_cat<3)
gen csr_hi = csr_lag*(wb_cat>=3)
label var csr_lo "Child survival, gen[-1], low income"
label var csr_hi "Child survival, gen[-1], high income"

tab year if year>=1985, gen(yd) // create year dummies after 1985
drop yd1
/**/ 
// -------------  1) using WDI GDP ínstead PWT GDP  ------------- //

local exoglag 4
local gvars "gWDI5 lgdppwWDI PRI trade_open yr_sch yd*"
local xtabgmm "gmm(lgdppw, laglimits(3 6))"
local xtabiv "iv(PRI trade_open yr_sch yd*) twostep robust"
local outopts "se varlabel squarebrack drop(yd*) starlevels(5 1 .1) noautosumm"

// e0
xtabond2 `gvars' e0, `xtabgmm' gmm(e0, laglimits(1 `exoglag')) `xtabiv'
xt_r2total
local star1 = cond(`=e(ar1p)' <= 0.01, "**", cond(`=e(ar1p)' <= 0.05, "*", ""))
local star2 = cond(`=e(ar2p)' <= 0.01, "**", cond(`=e(ar2p)' <= 0.05, "*", ""))
outreg, `outopts' nodisplay 	///
	ctitle("", "(1)" \ "", "Life" \ "", "expectancy")  /// 	
	addrow("Year effects", "yes" \ "N", "`=e(N)'" ///
		\ "Countries", "`:display %3.0f e(N_g)'"  ///
		\ "\(R^2 ({Corr(\hat{y},y)}^2)\)", "`:display %4.2f e(r2_t)'" 	///
		\ "ABond {\it z} test for AR(1)", "`:display %4.2f e(ar1)'`star1'" ///
		\ "ABond {\it z} test for AR(2)", "`:display %4.2f e(ar2)'`star2'" 	///
		\ "Hansen overid \(\chi^2\) test", "`:display %4.2f e(hansen)'") 

// asr & csr
xtabond2 `gvars' asr csr, `xtabgmm' gmm(asr csr, laglimits(1 `exoglag')) ///
	`xtabiv' 
xt_r2total
local star1 = cond(`=e(ar1p)' <= 0.01, "**", cond(`=e(ar1p)' <= 0.05, "*", ""))
local star2 = cond(`=e(ar2p)' <= 0.01, "**", cond(`=e(ar2p)' <= 0.05, "*", ""))

sum csr if e(sample)
scalar csr_sd = r(sd)
qui sum asr if e(sample)
scalar asr_sd = r(sd)

outreg, `outopts' merge nodisplay 	///
	ctitle("", "(2)" \ "", "Current" \ "", "CSR")  /// 	
	addrow("Year effects", "yes" \ "N", "`=e(N)'" ///
		\ "Countries", "`:display %3.0f e(N_g)'"  ///
		\ "\(R^2 ({Corr(\hat{y},y)}^2)\)", "`:display %4.2f e(r2_t)'" 	///
		\ "ABond {\it z} test for AR(1)", "`:display %4.2f e(ar1)'`star1'" ///
		\ "ABond {\it z} test for AR(2)", "`:display %4.2f e(ar2)'`star2'" 	///
		\ "Hansen overid \(\chi^2\) test", "`:display %4.2f e(hansen)'" ///
		\ "\% Growth from 1 s.d. $\Delta$ CSR",	///
		"`:display %4.2f csr_sd*_b[csr]'"		///		
		\ "\% Growth from 1 s.d. $\Delta$ ASR",	///
		"`:display %4.2f asr_sd*_b[asr]'")
 
// asr & csr_lag
di `"xtabond2 `gvars' asr csr_lag, `xtabgmm' iv(csr_lag) gmm(asr, laglimits(1 `exoglag')) `xtabiv'"'
xtabond2 `gvars' asr csr_lag, `xtabgmm' iv(csr_lag) gmm(asr, laglimits(1 `exoglag')) `xtabiv'
xt_r2total
local star1 = cond(`=e(ar1p)' <= 0.01, "**", cond(`=e(ar1p)' <= 0.05, "*", ""))
local star2 = cond(`=e(ar2p)' <= 0.01, "**", cond(`=e(ar2p)' <= 0.05, "*", ""))

sum csr_lag if e(sample)
scalar csr_sd = r(sd)
qui sum asr if e(sample)
scalar asr_sd = r(sd)
sum trade_open if e(sample)
di "growth from 1 s.d. trade: `:display %4.2f r(sd)*_b[trade_open]'"

outreg, `outopts' merge /// nodisplay	
	ctitle("", "(3)" \ "", "Lagged" \ "", "CSR")  ///
	addrow("Year effects", "yes" \ "N", "`=e(N)'" ///
		\ "Countries", "`:display %3.0f e(N_g)'"  ///
		\ "\(R^2 ({Corr(\hat{y},y)}^2)\)", "`:display %4.2f e(r2_t)'" 	///
		\ "ABond {\it z} test for AR(1)", "`:display %4.2f e(ar1)'`star1'" ///
		\ "ABond {\it z} test for AR(2)", "`:display %4.2f e(ar2)'`star2'" 	///
		\ "Hansen overid \(\chi^2\) test", "`:display %4.2f e(hansen)'" ///
		\ "\% Growth from 1 s.d. $\Delta$ CSR",	///
		"`:display %4.2f csr_sd*_b[csr_lag]'"	///		
		\ "\% Growth from 1 s.d. $\Delta$ ASR",	///
		"`:display %4.2f asr_sd*_b[asr]'")

// csr_lo and csr_hi
xtabond2 `gvars' asr csr_lo csr_hi, `xtabgmm' iv(csr_lo csr_hi) gmm(asr, laglimits(1 `exoglag')) `xtabiv'
xt_r2total
local star1 = cond(`=e(ar1p)' <= 0.01, "**", cond(`=e(ar1p)' <= 0.05, "*", ""))
local star2 = cond(`=e(ar2p)' <= 0.01, "**", cond(`=e(ar2p)' <= 0.05, "*", ""))

sum csr_lo if e(sample) & csr_lo>0
di "Low income CSR mean = `=r(mean)'; s.d. = `=r(sd)'"
scalar csrl_sd = r(sd)
qui sum csr_hi if e(sample) & csr_hi>0
di "High income CSR mean = `=r(mean)'; s.d. = `=r(sd)'"
scalar csrh_sd = r(sd)

outreg using ../../draft/images/WDI_e0_csr, tex frag `outopts' merge ///
	ctitle("", "(4)" \ "", "Low vs." \ "", "High Income")  ///
	addrow("Year effects", "yes" \ "N", "`=e(N)'" ///
		\ "Countries", "`:display %3.0f e(N_g)'"  ///
		\ "\(R^2 ({Corr(\hat{y},y)}^2)\)", "`:display %4.2f e(r2_t)'" ///
		\ "ABond {\it z} test for AR(1)", "`:display %4.2f e(ar1)'`star1'" ///
		\ "ABond {\it z} test for AR(2)", "`:display %4.2f e(ar2)'`star2'" 	///
		\ "Hansen overid \(\chi^2\) test", "`:display %4.2f e(hansen)'" ///
		\ "\% Growth from 1 s.d. $\Delta$ CSR L.I.",	///
		"`:display %4.2f csrl_sd*_b[csr_lo]'"			///		
		\ "\% Growth from 1 s.d. $\Delta$ CSR H.I.",	///
		"`:display %4.2f csrh_sd*_b[csr_hi]'")

		
// -------------  2) no endogenous e0 or asr  ------------- //

local gvars "gPWT5 lgdppw PRI trade_open yr_sch yd*"
local xtabgmm "gmm(lgdppw, laglimits(3 6))"
local xtabiv "iv(PRI trade_open yr_sch yd*) twostep robust"
local outopts "se varlabel squarebrack drop(yd*) starlevels(5 1 .1) noautosumm"

// e0
quietly{
xtabond2 `gvars' e0, `xtabgmm' iv(e0) `xtabiv'
xt_r2total
local star1 = cond(`=e(ar1p)' <= 0.01, "**", cond(`=e(ar1p)' <= 0.05, "*", ""))
local star2 = cond(`=e(ar2p)' <= 0.01, "**", cond(`=e(ar2p)' <= 0.05, "*", ""))
outreg, `outopts' nodisplay 	///
	ctitle("", "(1)" \ "", "Life" \ "", "expectancy")  /// 	
	addrow("Year effects", "yes" \ "N", "`=e(N)'" ///
		\ "Countries", "`:display %3.0f e(N_g)'"  ///
		\ "\(R^2 ({Corr(\hat{y},y)}^2)\)", "`:display %4.2f e(r2_t)'" 	///
		\ "ABond {\it z} test for AR(1)", "`:display %4.2f e(ar1)'`star1'" ///
		\ "ABond {\it z} test for AR(2)", "`:display %4.2f e(ar2)'`star2'" 	///
		\ "Hansen overid \(\chi^2\) test", "`:display %4.2f e(hansen)'") 

// asr & csr
xtabond2 `gvars' asr csr, `xtabgmm' iv(asr csr) ///
	`xtabiv' 
xt_r2total
local star1 = cond(`=e(ar1p)' <= 0.01, "**", cond(`=e(ar1p)' <= 0.05, "*", ""))
local star2 = cond(`=e(ar2p)' <= 0.01, "**", cond(`=e(ar2p)' <= 0.05, "*", ""))

sum csr if e(sample)
scalar csr_sd = r(sd)
qui sum asr if e(sample)
scalar asr_sd = r(sd)

outreg, `outopts' merge nodisplay 	///
	ctitle("", "(2)" \ "", "Current" \ "", "CSR")  /// 	
	addrow("Year effects", "yes" \ "N", "`=e(N)'" ///
		\ "Countries", "`:display %3.0f e(N_g)'"  ///
		\ "\(R^2 ({Corr(\hat{y},y)}^2)\)", "`:display %4.2f e(r2_t)'" 	///
		\ "ABond {\it z} test for AR(1)", "`:display %4.2f e(ar1)'`star1'" ///
		\ "ABond {\it z} test for AR(2)", "`:display %4.2f e(ar2)'`star2'" 	///
		\ "Hansen overid \(\chi^2\) test", "`:display %4.2f e(hansen)'" ///
		\ "\% Growth from 1 s.d. $\Delta$ CSR",	///
		"`:display %4.2f csr_sd*_b[csr]'"		///		
		\ "\% Growth from 1 s.d. $\Delta$ ASR",	///
		"`:display %4.2f asr_sd*_b[asr]'")
 
// asr & csr_lag
xtabond2 `gvars' asr csr_lag, `xtabgmm' iv(asr csr_lag) `xtabiv'
xt_r2total
local star1 = cond(`=e(ar1p)' <= 0.01, "**", cond(`=e(ar1p)' <= 0.05, "*", ""))
local star2 = cond(`=e(ar2p)' <= 0.01, "**", cond(`=e(ar2p)' <= 0.05, "*", ""))

sum csr_lag if e(sample)
scalar csr_sd = r(sd)
qui sum asr if e(sample)
scalar asr_sd = r(sd)
sum trade_open if e(sample)
di "growth from 1 s.d. trade: `:display %4.2f r(sd)*_b[trade_open]'"

outreg, `outopts' merge /// nodisplay	
	ctitle("", "(3)" \ "", "Lagged" \ "", "CSR")  ///
	addrow("Year effects", "yes" \ "N", "`=e(N)'" ///
		\ "Countries", "`:display %3.0f e(N_g)'"  ///
		\ "\(R^2 ({Corr(\hat{y},y)}^2)\)", "`:display %4.2f e(r2_t)'" 	///
		\ "ABond {\it z} test for AR(1)", "`:display %4.2f e(ar1)'`star1'" ///
		\ "ABond {\it z} test for AR(2)", "`:display %4.2f e(ar2)'`star2'" 	///
		\ "Hansen overid \(\chi^2\) test", "`:display %4.2f e(hansen)'" ///
		\ "\% Growth from 1 s.d. $\Delta$ CSR",	///
		"`:display %4.2f csr_sd*_b[csr_lag]'"	///		
		\ "\% Growth from 1 s.d. $\Delta$ ASR",	///
		"`:display %4.2f asr_sd*_b[asr]'")

// csr_lo and csr_hi
xtabond2 `gvars' asr csr_lo csr_hi, `xtabgmm' iv(asr csr_lo csr_hi) `xtabiv'
xt_r2total
local star1 = cond(`=e(ar1p)' <= 0.01, "**", cond(`=e(ar1p)' <= 0.05, "*", ""))
local star2 = cond(`=e(ar2p)' <= 0.01, "**", cond(`=e(ar2p)' <= 0.05, "*", ""))

sum csr_lo if e(sample) & csr_lo>0
di "Low income CSR mean = `=r(mean)'; s.d. = `=r(sd)'"
scalar csrl_sd = r(sd)
qui sum csr_hi if e(sample) & csr_hi>0
di "High income CSR mean = `=r(mean)'; s.d. = `=r(sd)'"
scalar csrh_sd = r(sd)

} // end quietly

outreg using ../../draft/images/exog_asr, tex frag `outopts' merge ///
	ctitle("", "(4)" \ "", "Low vs." \ "", "High Income")  ///
	addrow("Year effects", "yes" \ "N", "`=e(N)'" ///
		\ "Countries", "`:display %3.0f e(N_g)'"  ///
		\ "\(R^2 ({Corr(\hat{y},y)}^2)\)", "`:display %4.2f e(r2_t)'" ///
		\ "ABond {\it z} test for AR(1)", "`:display %4.2f e(ar1)'`star1'" ///
		\ "ABond {\it z} test for AR(2)", "`:display %4.2f e(ar2)'`star2'" 	///
		\ "Hansen overid \(\chi^2\) test", "`:display %4.2f e(hansen)'" ///
		\ "\% Growth from 1 s.d. $\Delta$ CSR L.I.",	///
		"`:display %4.2f csrl_sd*_b[csr_lo]'"			///		
		\ "\% Growth from 1 s.d. $\Delta$ CSR H.I.",	///
		"`:display %4.2f csrh_sd*_b[csr_hi]'")
/**/
		
// -------------  3) no yr_sch  ------------- //

// regression with yr_sch for comparison
//qui{
local gvars "gPWT5 lgdppw PRI trade_open yr_sch yd* asr csr_lag"
local exoglag 4
local xtabgmm "gmm(lgdppw, laglimits(3 6))"
local xtabiv "iv(csr_lag PRI trade_open yr_sch yd*) twostep robust"
local outopts "se varlabel squarebrack drop(yd*) starlevels(5 1 .1) noautosumm"

xtabond2 `gvars', `xtabgmm' `xtabiv' gmm(asr, laglimits(1 `exoglag')) 
xt_r2total
local star1 = cond(`=e(ar1p)' <= 0.01, "**", cond(`=e(ar1p)' <= 0.05, "*", ""))
local star2 = cond(`=e(ar2p)' <= 0.01, "**", cond(`=e(ar2p)' <= 0.05, "*", ""))

sum csr_lag if e(sample)
scalar csr_sd = r(sd)
qui sum asr if e(sample)
scalar asr_sd = r(sd)
outreg, `outopts' ctitle("" "with" \ "" "Schooling") nodisplay  ///
	addrow("Year effects", "yes" \ "N", "`=e(N)'" ///
		\ "Countries", "`:display %3.0f e(N_g)'"  ///
		\ "\(R^2 ({Corr(\hat{y},y)}^2)\)", "`:display %4.2f e(r2_t)'" 	///
		\ "ABond {\it z} test for AR(1)", "`:display %4.2f e(ar1)'`star1'" ///
		\ "ABond {\it z} test for AR(2)", "`:display %4.2f e(ar2)'`star2'" 	///
		\ "Hansen overid \(\chi^2\) test", "`:display %4.2f e(hansen)'" ///
		\ "\% Growth from 1 s.d. $\Delta$ CSR",	///
		"`:display %4.2f csr_sd*_b[csr_lag]'"	///		
		\ "\% Growth from 1 s.d. $\Delta$ ASR",	///
		"`:display %4.2f asr_sd*_b[asr]'")
scalar b_w_sch = _b[csr_lag]
gen byte sch_smpl = e(sample)


//  regression without yr_sch
qui {
local gvars "gPWT5 lgdppw PRI trade_open yd* asr csr_lag"
local xtabiv "iv(csr_lag PRI trade_open yd*) twostep robust"
xtabond2 `gvars' if sch_smpl, `xtabgmm' `xtabiv'	///
	gmm(asr, laglimits(1 `exoglag')) 
xt_r2total
local star1 = cond(`=e(ar1p)' <= 0.01, "**", cond(`=e(ar1p)' <= 0.05, "*", ""))
local star2 = cond(`=e(ar2p)' <= 0.01, "**", cond(`=e(ar2p)' <= 0.05, "*", ""))

sum csr_lag if e(sample)
scalar csr_sd = r(sd)
sum asr if e(sample)
scalar asr_sd = r(sd)
}		
outreg using ../../draft/images/no_sch, tex frag `outopts' merge 	///
	ctitle("" "without" \ "" "Schooling") 	///
	addrow("Year effects", "yes" \ "N", "`=e(N)'" ///
		\ "Countries", "`:display %3.0f e(N_g)'"  ///
		\ "\(R^2 ({Corr(\hat{y},y)}^2)\)", "`:display %4.2f e(r2_t)'" 	///
		\ "ABond {\it z} test for AR(1)", "`:display %4.2f e(ar1)'`star1'" ///
		\ "ABond {\it z} test for AR(2)", "`:display %4.2f e(ar2)'`star2'" 	///
		\ "Hansen overid \(\chi^2\) test", "`:display %4.2f e(hansen)'" ///
		\ "\% Growth from 1 s.d. $\Delta$ CSR",	///
		"`:display %4.2f csr_sd*_b[csr_lag]'"	///		
		\ "\% Growth from 1 s.d. $\Delta$ ASR",	///
		"`:display %4.2f asr_sd*_b[asr]'")

di "%↑ = `=(_b[csr_lag]/b_w_sch - 1)*100'"
		
// -------------  4) yr_sch but no csr (or e0)  ------------- //

// regression with csr_lag for comparison
//qui{
local gvars "gPWT5 lgdppw PRI trade_open yr_sch yd* asr csr_lag"
local exoglag 4
local xtabgmm "gmm(lgdppw, laglimits(3 6))"
local xtabiv "iv(csr_lag PRI trade_open yr_sch yd*) twostep robust"
local outopts "se varlabel squarebrack drop(yd*) starlevels(5 1 .1) noautosumm"

xtabond2 `gvars', `xtabgmm' `xtabiv' gmm(asr, laglimits(1 `exoglag')) 
xt_r2total
local star1 = cond(`=e(ar1p)' <= 0.01, "**", cond(`=e(ar1p)' <= 0.05, "*", ""))
local star2 = cond(`=e(ar2p)' <= 0.01, "**", cond(`=e(ar2p)' <= 0.05, "*", ""))

sum csr_lag if e(sample)
scalar csr_sd = r(sd)
qui sum asr if e(sample)
scalar asr_sd = r(sd)
outreg, `outopts' ctitle("" "with" \ "" "Child Survival") nodisplay  ///
	addrow("Year effects", "yes" \ "N", "`=e(N)'" ///
		\ "Countries", "`:display %3.0f e(N_g)'"  ///
		\ "\(R^2 ({Corr(\hat{y},y)}^2)\)", "`:display %4.2f e(r2_t)'" 	///
		\ "ABond {\it z} test for AR(1)", "`:display %4.2f e(ar1)'`star1'" ///
		\ "ABond {\it z} test for AR(2)", "`:display %4.2f e(ar2)'`star2'" 	///
		\ "Hansen overid \(\chi^2\) test", "`:display %4.2f e(hansen)'")
scalar b_w_csr = _b[yr_sch]
gen byte csr_smpl = e(sample)

// regression without csr_lag
local gvars "gPWT5 lgdppw PRI trade_open yr_sch asr yd*"
xtabond2 `gvars' if csr_smpl, `xtabgmm' gmm(asr, laglimits(1 `exoglag')) `xtabiv'
xt_r2total
local star1 = cond(`=e(ar1p)' <= 0.01, "**", cond(`=e(ar1p)' <= 0.05, "*", ""))
local star2 = cond(`=e(ar2p)' <= 0.01, "**", cond(`=e(ar2p)' <= 0.05, "*", ""))

outreg using ../../draft/images/just_sch, tex frag merge replace ///
	`outopts' ctitle("" "without" \ "" "Child Survival") 	/// 
	addrow("Year effects", "yes" \ "N", "`=e(N)'" ///
		\ "Countries", "`:display %3.0f e(N_g)'" ///
		\ "\(R^2 ({Corr(\hat{y},y)}^2)\)", "`:display %4.2f e(r2_t)'" ///
		\ "ABond {\it z} test for AR(1)", "`:display %4.2f e(ar1)'`star1'" ///
		\ "ABond {\it z} test for AR(2)", "`:display %4.2f e(ar2)'" ///
		\ "Hansen overid \(\chi^2\) test", "`:display %4.2f e(hansen)'" ///
		\ "Ratio of Schooling without vs. with CSR", 	///
		"`:display %3.1f _b[yr_sch]/b_w_csr'")
		